/*
 * pps_bom
 */
package com.pitop.bom.service;

import com.pitop.bom.api.BomFeignApi;
import com.pitop.bom.entity.Bom;
import com.pitop.bom.entity.BomState;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.BomException;
import com.pitop.core.tools.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * bom
 *
 * @author berton
 */
@Service("bomSVImpl")
@Slf4j
public class BomSVImpl {

    @Autowired
    private BomFeignApi bomFeignApi;

    /**
     * 加载一个对象Bom
     *
     * @param id * @param code 编码
     * @return Bom
     */
    public Bom load(Long id, String code) {
        if (id == null && code == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.load(id, code);
    }

    /**
     * 加载一个对象Bom 通过id,(将查询关联数据)
     *
     * @param id
     * @return Bom
     */
    public Bom loadById(Long id) {
        if (id == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Bom 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Bom
     */
    public Bom loadByCode(String code) {
        if (code == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象Bom 通过bomCode
     *
     * @param bomCode 编码
     * @return Bom
     */
    public Bom loadByBomCode(String bomCode) {
        if (bomCode == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.loadByBomCode(bomCode);
    }

    /**
     * 加载一个对象Bom,(将查询关联数据)
     *
     * @param id * @param code 编码
     * @return Bom
     */
    public Bom get(Long id, String code) {
        if (id == null && code == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.get(id, code);
    }

    /**
     * 加载一个对象Bom 通过id,(将查询关联数据)
     *
     * @param id
     * @return Bom
     */
    public Bom getById(Long id) {
        if (id == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.getById(id);
    }

    /**
     * 加载一个对象Bom 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Bom
     */
    public Bom getByCode(String code) {
        if (code == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.getByCode(code);
    }

    /**
     * 删除对象Bom
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        bomFeignApi.delete(id, code);
    }

    /**
     * 查询Bom分页
     *
     * @param bom      对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Bom>
     */
    public List<Bom> list(Bom bom, int curPage, int pageSize) {
        return bomFeignApi.list(bom, curPage, pageSize);
    }

    /**
     * 查询Bom分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Bom>
     */
    public List<Bom> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Bom bom = new Bom();
        bom.setId(id);
        bom.setCode(code);

        return bomFeignApi.list(bom, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return bomFeignApi.count(id, code);
    }

    public int count(Bom bom) {
        return bomFeignApi.count(bom);
    }

    /**
     * 保存
     * 1.参数合法性验证
     * 2.如果有上级bom，查询上级bom
     * 3.bom名称是否已经存在
     *
     * @param bom     实体
     * @param account
     * @param code    @throws BaseException
     */
    public void save(Bom bom, String account, String code) {
        //1.参数合法性验证
        if (bom == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(bom.getName()) || StringTools.isEmpty(bom.getBomCode())) {
            throw new BomException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //2.上级bom是否存在
        if(StringTools.isNotEmpty(bom.getPreCode())) {
            Bom fatherBom = bomFeignApi.loadByCode(bom.getPreCode());
            if(fatherBom == null) {
                throw new BomException(BaseException.BaseExceptionEnum.Father_Bom_Not_Exist);
            }
        }
        //3.查询名称是否已经被使用
        Bom bomFlag = bomFeignApi.loadByName(bom.getName());
        if(bomFlag != null) {
            throw new BomException(BaseException.BaseExceptionEnum.Bom_Name_Is_Already_Exist);
        }
        //4.查询bom代码是否存在
        bomFlag = bomFeignApi.loadByBomCode(bom.getBomCode());
        if(bomFlag != null) {
            throw new BomException(BaseException.BaseExceptionEnum.Bom_Code_Is_Already_Exist);
        }
        //5.保存
        bomFeignApi.build(bom, account, code);
    }

    /**
     * 更新
     *
     * @param bom     实体
     * @param account
     * @param code    @throws BaseException
     */
    public void modify(Bom bom, String account, String code) {
        //1.参数合法性验证
        if (bom == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(bom.getName()) || StringTools.isEmpty(bom.getBomCode())) {
            throw new BomException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //2.上级bom是否存在
        if(StringTools.isNotEmpty(bom.getPreCode())) {
            Bom fatherBom = bomFeignApi.loadByCode(bom.getPreCode());
            if(fatherBom == null) {
                throw new BomException(BaseException.BaseExceptionEnum.Father_Bom_Not_Exist);
            }
        }
        //3.查询名称是否已经被使用
        Bom bomFlag = bomFeignApi.loadByName(bom.getName());
        if(bomFlag != null && !bom.getCode().equals(bomFlag.getCode())) {
            throw new BomException(BaseException.BaseExceptionEnum.Bom_Name_Is_Already_Exist);
        }
        //4.查询bom代码是否存在
        bomFlag = bomFeignApi.loadByBomCode(bom.getBomCode());
        if(bomFlag != null && !bom.getCode().equals(bomFlag.getCode())) {
            throw new BomException(BaseException.BaseExceptionEnum.Bom_Code_Is_Already_Exist);
        }
        bomFeignApi.modify(bom, account, code);
    }

    /**
     * 修改bom状态
     *
     * @param code          bom编码
     * @param state         状态
     * @return
     */
    public Bom updateState(String code, String state) {
        if (StringTools.isEmpty(code) || StringTools.isEmpty(state)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Bom bom = bomFeignApi.loadByCode(code);
        if(bom == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //不是开发状态，不允许修改
        if(BomState.Develop != BomState.getEnum(bom.getState())) {
            throw new BomException(BaseException.BaseExceptionEnum.Bom_Not_Develop);
        }
        return bomFeignApi.updateState(code, state);
    }

    /**
     * 复制bom
     * 1.参数合法性验证
     * 2.是否已经复制过
     *
     * @param code        bom编码
     * @param account
     * @param accountCode @return
     */
    public Bom copy(String code, String account, String accountCode) {
        //1.参数合法性验证
        if (StringTools.isEmpty(code)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Bom bom = bomFeignApi.loadByCode(code);
        if (bom == null) {
            throw new BomException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //2.是否已经复制过
        Bom bomFlag = bomFeignApi.loadByName(bom.getName() + "_copy");
        if(bomFlag != null) {
            throw new BomException(BaseException.BaseExceptionEnum.Bom_has_been_copy);
        }
        if(StringTools.isNotEmpty(bom.getBomCode())) {
            bomFlag = bomFeignApi.loadByName(bom.getBomCode() + "_copy");
            if (bomFlag != null) {
                throw new BomException(BaseException.BaseExceptionEnum.Bom_has_been_copy);
            }
        }

        return bomFeignApi.copy(code, account, accountCode);
    }

    /**
     * 查询未绑定此成品的bom
     *
     * @param productCode 成品编码
     * @param name
     * @param bomCode
     * @param curPage     当前页
     * @param pageSize    分页大小   @return
     */
    public List<Bom> queryUnBindProductBomList(String productCode, String name, String bomCode, int curPage, int pageSize) {
        if (StringTools.isEmpty(productCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.queryUnBindProductBomList(productCode, name, bomCode, curPage, pageSize);
    }

    public int countUnBindProductBomList(String productCode, String name, String bomCode) {
        if (StringTools.isEmpty(productCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.countUnBindProductBomList(productCode, name, bomCode);
    }


    /**
     * 查询已绑定此成品的bom
     *
     * @param productCode 成品编码
     * @param name
     * @param bomCode
     * @param curPage     当前页
     * @param pageSize    分页大小   @return
     */
    public List<Bom> queryBindProductBomList(String productCode, String name, String bomCode, int curPage, int pageSize) {
        if (StringTools.isEmpty(productCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.queryBindProductBomList(productCode, name, bomCode, curPage, pageSize);
    }

    public int countBindProductBomList(String productCode, String name, String bomCode) {
        if (StringTools.isEmpty(productCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.countBindProductBomList(productCode, name, bomCode);
    }


    /**
     * 查询未绑定项目的bom
     *
     * @param projectCode 项目编码
     * @param name        bom名称
     * @param bomCode     bom编码
     * @param curPage     当前页
     * @param pageSize    分页大小
     * @return
     */
    public List<Bom> queryUnBindProjectBomList(String projectCode, String name, String bomCode, int curPage, int pageSize) {
        if (StringTools.isEmpty(projectCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.queryUnBindProjectBomList(projectCode, name, bomCode, curPage, pageSize);
    }

    public int countUnBindProjectBomList(String projectCode, String name, String bomCode) {
        if (StringTools.isEmpty(projectCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.countUnBindProjectBomList(projectCode, name, bomCode);
    }

    /**
     * 查询已绑定项目的bom列表
     *
     * @param projectCode 项目编码
     * @param name        bom名称
     * @param bomCode     bom编码
     * @param curPage     当前页
     * @param pageSize    分页大小
     * @return
     */
    public List<Bom> queryBindProjectBomList(String projectCode, String name, String bomCode, int curPage, int pageSize) {
        if (StringTools.isEmpty(projectCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.queryBindProjectBomList(projectCode, name, bomCode, curPage, pageSize);
    }

    public int countBindProjectBomList(String projectCode, String name, String bomCode) {
        if (StringTools.isEmpty(projectCode)) {
            throw new BomException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomFeignApi.countBindProjectBomList(projectCode, name, bomCode);
    }
}
